<template>
 <div class="app-container">
  <h3>投诉/禁言类别及监控次数设置</h3>
  <el-form :model="cateForm"  ref="cateForm"  class="demo-form-inline">
    <el-form-item label="总投诉监控次数:" label-width="120px">
      <el-col :span="4">
        <el-input v-model="cateForm.reportSize"  placeholder="请输入" @keyup.enter.native="toSave('cateForm')"></el-input>
      </el-col>
      </el-form-item>
       <el-form-item >
  <el-button class="add-item " type="success" @click="AddInput(1)">新增类别</el-button>
  </el-form-item>
      <el-form-item
        v-for="(cate, index) in cateForm.cateList"
        label="类别名："
        :key="cate.id"
        :prop="'cateList.' + index + '.name'"
      > <el-col :span="8">
        <el-input v-model="cate.name" @keyup.enter.native="toSave('cateForm')"></el-input>
        </el-col>
        <el-button type="danger" class="btn_danger"  @click="delInput(1,index)">删除</el-button>
        <!-- <el-button icon="el-icon-plus" circle v-if="index === 0" @click="addNewWork"></el-button> -->
      </el-form-item>
      <el-button class="filter-item" type="primary"  v-if="roleCheck('chatRoom-bannedType-save')" @click="toSave('cateForm')">保存</el-button>
    </el-form>

  <div class="line"></div>
  <!--禁言时间设置-->
   <h3>禁言时间设置</h3>
  <el-button class="add-item" type="success" @click="AddInput(2)">新增时间</el-button>
  <el-form  ref="banForm" :model="form" :rules="rule" class="demo-form-inline">
    <el-form-item class="with-pre-select"  v-for="(item,index) in form.bannedTime" :key="item.id"  >
      <el-col :span="8" class="readonly" v-if="Number(item.unit) === 0" >永久</el-col>
      <el-form-item :prop="'bannedTime.' + index + '.value'" :rules="rule.value" v-if="index!=0">
        <el-col :span="8" >
          <el-input placeholder="请输入时间" v-model="item.value" class="input-with-select"  @keyup.enter.native="toSave('banForm')">
            <el-select v-model="item.unit" slot="append" placeholder="全部">
              <el-option
              v-for="item1 in consts.CHATROOM_BAN_TIME"
              :label="item1.title"
              :key="item1.value"
              :value="item1.value"
              v-if="item1.value!=0"
            ></el-option>
            </el-select>
          </el-input>
          </el-col>
          <el-button type="danger" class="btn_danger"  @click="delInput(2,index)">删除</el-button>
      </el-form-item>
    </el-form-item>
  <el-button class="filter-item" type="primary"   v-if="roleCheck('chatRoom-bannedTime-save')"  @click="toSave('banForm')">保存</el-button>
  </el-form>
</div>

</template>

<script>

import {
  addCate, getcate, getBannedTime, setBanTime, getReportSize, setReportSize,
} from '@/services/api/chatRoom';
import { form, factory } from '@/build';


export default {
  name: 'chatRoom-banned-set',
  mixins: [form, factory],
  data() {
    return {
      reportSizeDefault: '',
      cateForm: {
        reportSize: undefined,
        cateList: [],
      },
      form: {
        bannedTime: [],
      }, // 禁言时间列表
      rule: {
        value: [
          { required: true, message: '请输入时间', trigger: 'change' },
          { validator: factory.getValidator('positiveInteger'), trigger: 'change' },
        ],
      },
    };
  },
  mounted() {
    this.bindGet(getcate, '', {
      action: 'catetType',
      callback: (data) => {
        this.cateForm.cateList = data[1];
      },
    });
    this.bindGet(getBannedTime, '', {
      action: 'getBannedTime',
      callback: (data) => {
        this.form.bannedTime = data[1];
      },
    });
    this.bindGet(getReportSize, '', {
      action: 'getReportSize',
      callback: (data) => {
        this.cateForm.reportSize = data[1].report_size;
        this.reportSizeDefault = data[1].report_size;
      },
    });
    this.getAll();
    this.bindSave(addCate, 'addCate', '', {
      beforeSend: () => {
        const data = {
          uid: this.$store.state.user.userInfo.userId,
          account: this.$store.state.user.userInfo.username,
          name: this.cateForm.cateList.filter(value => value.name !== '').map(item => item.name),
        };
        return data;
      },
    });
    this.bindSave(setBanTime, 'setBanTime', '', {
      beforeSend: () => {
        const data = {
          uid: this.$store.state.user.userInfo.userId,
          account: this.$store.state.user.userInfo.username,
          ttl: this.form.bannedTime.filter(value => value.value !== '').map(item => ({ value: item.value, unit: Number(item.unit) })),
        };
        return data;
      },
    });
    this.bindSave(setReportSize, 'setReportSize', '', {
      beforeSend: () => {
        const data = {
          report_size: this.cateForm.reportSize,
        };
        return data;
      },
    });
  },
  methods: {
    AddInput(type) {
      if (type === 1) { // 增加禁言类别
        this.cateForm.cateList.push({
          name: '',
        });
      } else { // 增加禁言时间
        this.form.bannedTime.push({
          ttl: '',
          unit: 1,
        });
      }
    },
    delInput(type, currIndex) {
      if (type === 1) { // 禁言类别
        this.cateForm.cateList = this.cateForm.cateList.filter((value, index) => index !== currIndex);
      } else {
        this.form.bannedTime = this.form.bannedTime.filter((value, index) => index !== currIndex);
      }
    },
    async toSave(formName) {
      this.$nextTick(() => {
        if (formName === 'banForm') {
          // this.$refs.spacial.clearValidate();
          this.$refs.banForm.validate((valid) => {

            if (valid) {
              this.doSave({ action: 'setBanTime', form: formName });
            }
          });

        } else {
          this.doSave({ action: 'addCate', form: formName });
          if (this.reportSizeDefault !== this.cateForm.reportSize) {
            this.doSave({ action: 'setReportSize', form: formName });
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.add-item{margin-top:10px}
.demo-form-inline{margin-top:30px}
.line{height:2px;background: #eee;margin-top:50px;margin-bottom:40px}
.readonly{width:150px;height:30px;line-height: 30px;background: #eee;text-align: center;color:#999;border-radius: 3px;}
.btn_danger{
  margin-left: 20px;
}
 .with-pre-select {
    .el-input-group__append {
      .el-select {
        width: 120px;
      }
    }
  }
</style>
